#!/usr/bin/python2
# -*- coding: us-ascii -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab

"""
webdriver wrapper tests
-----------------------

Test cases for the Pycopia webdriver wrapper.

"""

from pycopia.QA import core
from pycopia.QA import webdriver

__author__ = "keith@dartworks.biz"


class PycopiaTest(core.Test):
    """
    Purpose
    +++++++

    Verify the webdriver by loading the www.pycopia.org webiste page.

    Pass Criteria
    +++++++++++++

    Pycopia home page is loaded and detected.

    Start Condition
    +++++++++++++++

    Pycopia website is the DUT in the environment.

    End Condition
    +++++++++++++

    Browser contains pycopia.org home page.

    Reference
    +++++++++

    None

    Prerequistes
    ++++++++++++

    None

    Procedure
    +++++++++

    # Load the main page of the environment's DUT, which should have been
      configured to be pycopia.org.
    # Verify "Python Application Frameworks" is found in title.
    """

    def execute(self):
        wd = self.config.webdriver
        target_url = "http://www.pycopia.org"
        wd.get(target_url)
        self.failUnless(wd.title.startswith("Pycopia reference documentation"), 
                "Title: %r not correct.".format(wd.title))
        return self.passed("Got correct title page.")


class PycopiaQATest(core.Test):
    """
    Purpose
    +++++++

    Verify webdriver path selection by fetching Pycopia website's tools page.

    Pass Criteria
    +++++++++++++

    Pycopia web tools page loads, verified with string "Web Utilities and
    Tools" contained in the first H1 tag.

    Start Condition
    +++++++++++++++

    Base page has been loaded.

    End Condition
    +++++++++++++

    Browser contains /webtools page.

    Reference
    +++++++++

    None

    Prerequiste
    +++++++++++

    PycopiaTest

    Procedure
    +++++++++

    - Load the /webtools locatation (append to prior URL).
    - Fetch first H1 tag.
    - Verify "Web Utilities and Tools" is found in H1 text.

    """
    PREREQUISITES = ["PycopiaTest"]

    def execute(self):
        wd = self.config.webdriver
        wd.get(wd.current_url + "webtools")
        headtext = wd.find_elements_by_tag_name("h1")[0].text
        self.failUnless(headtext == 'Web Utilities and Tools', "Wrong H1 text.")
        return self.passed("Got webtools path.")

def get_suite(config):
    suite = webdriver.WebdriverSuite(config, name="WebdriverUnitTestSuite")
    suite.add_test(PycopiaTest)
    suite.add_test(PycopiaQATest)
    return suite


def run(config):
    suite = get_suite(config)
    suite.run()

